package griffin.logic;

import android.content.Context;
import android.content.SharedPreferences;
//import android.content.ContextWrapper;
//import android.app.Activity;
//import android.content.Intent;
//import android.os.Bundle;
import android.util.Log;
//import android.view.View;
//import android.view.View.OnClickListener;
//import android.widget.Button;
//import android.widget.ImageView;
//import android.widget.TextView;
//import android.widget.EditText;
//import android.widget.CheckBox;
//import android.content.SharedPreferences;
//import android.content.Intent;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicBoolean;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

public class userLogic {
		private static final String TAG = "login";
	    private static final String SOAP_ACTION = "http://tempuri.org/login";
	    private static final String METHOD_NAME = "login";
	    private static final String NAMESPACE = "http://tempuri.org/";
	    private static final String URL = "http://griffin.ciandt.cn/Sservice1.asmx";
	    private Context context;
	    
		public userLogic(Context context) {
			this.context = context;
		}
	    
	    private enum CONSTANT_KEY {  
	        LOGIN_INFO("griffin.website.loginInfo"),
	        USERNAME("username"),
	        PASSWORD("password"),
	        USERID("userid"),
	        IS_REMEMBER_LOGIN("rememberLogin");
	        

	        private final String key;     
			private CONSTANT_KEY(String key) {  
			     this.key = key;  
			}
	    };    

		//set after userlogin( userid username password isRemember )
		public void setLoginUser(int userid,String username,String password,boolean isRemember){
			SharedPreferences loginInfo=context.getSharedPreferences(CONSTANT_KEY.LOGIN_INFO.key,0);
	    	SharedPreferences.Editor editor=loginInfo.edit();
	    	editor.putString(CONSTANT_KEY.USERNAME.key, username);
	    	editor.putString(CONSTANT_KEY.PASSWORD.key, password);
	    	editor.putInt(CONSTANT_KEY.USERID.key, userid);
	    	if(isRemember)
	    	{
	    		editor.putBoolean(CONSTANT_KEY.IS_REMEMBER_LOGIN.key, true);
//	    		Log.d(TAG,"[=========login with remember=========]" );	
	    	}else
	    	{
	    		if(loginInfo.contains(CONSTANT_KEY.IS_REMEMBER_LOGIN.key)){
	    			editor.remove(CONSTANT_KEY.IS_REMEMBER_LOGIN.key);
	    		}
//    			Log.d(TAG,"[=========login no remember=========]" );	
	    	}
	    	editor.commit();
		}
		//get userlogin( username password userid)
		public boolean getLoginUser(AtomicReference<String> username
				,AtomicReference<String> password
				,AtomicInteger userid){
			username.set("");
			password.set("");
			userid.set(0);
	    	SharedPreferences loginInfo=context.getSharedPreferences(CONSTANT_KEY.LOGIN_INFO.key, 0);
	    	if(!loginInfo.equals(null)&& loginInfo.contains(CONSTANT_KEY.USERNAME.key)
		    	&& loginInfo.contains(CONSTANT_KEY.PASSWORD.key)
		    	&& loginInfo.contains(CONSTANT_KEY.USERID.key)){
	    		username.set(loginInfo.getString(CONSTANT_KEY.USERNAME.key, ""));
	    		password.set(loginInfo.getString(CONSTANT_KEY.PASSWORD.key, "")); 
	    		userid.set(loginInfo.getInt(CONSTANT_KEY.USERID.key, 0)); 
	    		return true;
	    	}
	    	return false;
		}
		public boolean getLoginUser(AtomicReference<String> username
				,AtomicReference<String> password
				,AtomicBoolean isRemember){		
			username.set("");
			password.set("");
			isRemember.set(false);
	    	SharedPreferences loginInfo=context.getSharedPreferences(CONSTANT_KEY.LOGIN_INFO.key, 0);
	    	if(!loginInfo.equals(null)){
		    	if(loginInfo.contains(CONSTANT_KEY.USERNAME.key)
		    		&& loginInfo.contains(CONSTANT_KEY.PASSWORD.key)){
		    		username.set(loginInfo.getString(CONSTANT_KEY.USERNAME.key, ""));
		    		password.set(loginInfo.getString(CONSTANT_KEY.PASSWORD.key, ""));    		
		    	}
		    	if(loginInfo.contains(CONSTANT_KEY.IS_REMEMBER_LOGIN.key)){	    		
		    		isRemember.set(loginInfo.getBoolean(CONSTANT_KEY.IS_REMEMBER_LOGIN.key,false));
		    		return true;
		    	}
	    	}
			return false;
		}
		//login out
		public void loginOut(){
	    	SharedPreferences loginInfo=context.getSharedPreferences(CONSTANT_KEY.LOGIN_INFO.key, 0);
	    	if(loginInfo.equals(null))return;
	    	SharedPreferences.Editor editor=loginInfo.edit();
	    	if(loginInfo.contains(CONSTANT_KEY.USERNAME.key))editor.remove(CONSTANT_KEY.USERNAME.key);
	    	if(loginInfo.contains(CONSTANT_KEY.PASSWORD.key))editor.remove(CONSTANT_KEY.PASSWORD.key);
	    	if(loginInfo.contains(CONSTANT_KEY.USERID.key))editor.remove(CONSTANT_KEY.USERID.key);
	    	editor.commit();
	    	Log.d(TAG,"[=========loginOut suscefully=========]" );	    	
		}
		public void loginOutWhenExit(){
			SharedPreferences loginInfo=context.getSharedPreferences(CONSTANT_KEY.LOGIN_INFO.key, 0);
			//if no key or key value is false  clear up
			if(!(loginInfo.contains(CONSTANT_KEY.IS_REMEMBER_LOGIN.key)
				 && loginInfo.getBoolean(CONSTANT_KEY.IS_REMEMBER_LOGIN.key, false))){
				Log.d(TAG,"[=========loginout wiht remember=========]" );
			}else{
				Log.d(TAG,"[=========loginout no remember=========]" );	
				loginOut();
			}
		}
		//check username and password is correct
		public int checkLoginUser(String username,String password,AtomicReference<String> errorMessage){
			int result = 0;
			//test code
			if(username.equals("test") && password.equals("test"))
			return 1;
			//
			try{
// 				Log.d(TAG, "[Login...] ");	
				SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		        request.addProperty("username", username);
		        request.addProperty("password", password);

		        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		        envelope.dotNet=true;
		        envelope.setOutputSoapObject(request);

		        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
		        androidHttpTransport.call(SOAP_ACTION, envelope);

		        result = Integer.parseInt(((Object)envelope.getResponse()).toString());

		        if(result>0){
//		        	gologin(username.getText().toString(),result.toString());
		        }else{
		        	errorMessage.set("username or password is not correct");			        	
		        }	        
			}catch(Exception ex){
				errorMessage.set("login failed:" +ex.toString());
			}
			return result;
		}

}
